package Offer.offer2019;

import java.util.Stack;

/**
 * 包含min函数的栈
 * @author: leiwenkang02@meituan.com
 * @date: 2019/5/7
 * @time: 11:31
 */
public class Offer21 {

    Stack<Integer> stack = new Stack<>();
    //辅助栈
    Stack<Integer> minStack = new Stack<>();

    public void push(int value) {
         stack.push(value);
         if (minStack.isEmpty()) {
             minStack.push(value);
         } else {
             if (value < minStack.peek()) {
                 minStack.push(value);
             } else {
                 minStack.push(minStack.peek());
             }
         }
    }

    public int pop() {
        if (stack.isEmpty()) {
            throw new RuntimeException("栈为空！");
        }
        minStack.pop();
        return stack.pop();
    }

    public int min() {
        return minStack.peek();
    }
}
